package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.controller.demo.domain.SysEmail;
import com.ruoyi.web.controller.demo.domain.SysWzxUser;
import com.ruoyi.web.controller.service.ISysEmailService;
import com.ruoyi.web.controller.service.ISysWzxUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * sys_emailController
 *
 * @author ruoyi
 * @date 2022-12-22
 */
@Controller
@RequestMapping("/system/email")
public class SysEmailController extends BaseController
{
    private String prefix = "system/email";

    @Autowired
    private ISysEmailService sysEmailService;
    @Autowired
    private ISysWzxUserService sysWzxUserService;

    @RequiresPermissions("system:email:view")
    @GetMapping()
    public String email()
    {

        return prefix + "/email";
    }

    @GetMapping("/shouemail")
    public String shouemail()
    {

        return prefix + "/shouemail";
    }

    @GetMapping("/draft")
    public String draft()
    {
        return prefix + "/draft";
    }

    @GetMapping("/dash")
    public String dash()
    {
        return prefix + "/dash";
    }

    /**
     * 查询sys_email列表
     */
    @RequiresPermissions("system:email:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysEmail sysEmail)
    {
        startPage();
        SysUser sysUser = getSysUser();
        sysEmail.setOutId(sysUser.getUserId());
        List<SysEmail> list = sysEmailService.selectSysEmailList(sysEmail);
        return getDataTable(list);
    }

    @PostMapping("/showlist")
    @ResponseBody
    public TableDataInfo showList(SysEmail sysEmail)
    {
        startPage();
        SysUser sysUser = getSysUser();
        sysEmail.setInId(sysUser.getUserId());
        List<SysEmail> list = sysEmailService.selectSysEmailListShow(sysEmail);
        return getDataTable(list);
    }

    //草稿箱
    @PostMapping("/draftList")
    @ResponseBody
    public TableDataInfo draftList(SysEmail sysEmail)
    {
        startPage();
        SysUser sysUser = getSysUser();
        sysEmail.setOutId(sysUser.getUserId());
        List<SysEmail> list = sysEmailService.selectSysEmailListDraft(sysEmail);
        return getDataTable(list);
    }

    //垃圾箱
    @PostMapping("/dashList")
    @ResponseBody
    public TableDataInfo dashList(SysEmail sysEmail)
    {
        startPage();
        /*SysUser sysUser = getSysUser();
        sysEmail.setOutId(sysUser.getUserId());*/
        List<SysEmail> list = sysEmailService.selectSysEmailListDash(sysEmail);
        return getDataTable(list);
    }


    /**
     * 导出sys_email列表
     */
    @RequiresPermissions("system:email:export")
    @Log(title = "sys_email", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysEmail sysEmail)
    {
        List<SysEmail> list = sysEmailService.selectSysEmailList(sysEmail);
        ExcelUtil<SysEmail> util = new ExcelUtil<SysEmail>(SysEmail.class);
        return util.exportExcel(list, "sys_email数据");
    }

    /**
     * 新增sys_email
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        //给前端输入收件人列表
        List<SysWzxUser> list = sysWzxUserService.selectSysWzxUserList(null);
        model.addAttribute("user", list);

        SysUser sysUser = getSysUser();
        model.addAttribute("sysUser", sysUser);
        //给前端输入收件人列表

        return prefix + "/add";
    }

    /**
     * 新增保存sys_email
     */
    @RequiresPermissions("system:email:add")
    @Log(title = "sys_email", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysEmail sysEmail)
    {
//        sysEmailService.insertSysEmailIn(sysEmail);
        return toAjax(sysEmailService.insertSysEmail(sysEmail));
    }


    @Log(title = "sys_email", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult Save(SysEmail sysEmail)
    {
        return toAjax(sysEmailService.saveSysEmail(sysEmail));
    }
    /**
     * 修改sys_email
     */
    @RequiresPermissions("system:email:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysEmail sysEmail = sysEmailService.selectSysEmailById(id);
        mmap.put("sysEmail", sysEmail);
        return prefix + "/edit";
    }

    //草稿箱编辑
    @RequiresPermissions("system:email:edit")
    @GetMapping("/draftEdit/{id}")
    public String draftEdit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysEmail sysEmail = sysEmailService.selectSysEmailById(id);
        List<SysWzxUser> sysWzxUsers = sysWzxUserService.selectSysWzxUserList(null);
        SysUser sysUser = getSysUser();
        mmap.put("sysUser", sysUser);
        mmap.put("sysUsers", sysWzxUsers);
        mmap.put("sysEmail", sysEmail);
        return prefix + "/draftEdit";
    }

    /**
     * 修改保存sys_email
     */
    @RequiresPermissions("system:email:edit")
    @Log(title = "sys_email", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysEmail sysEmail)
    {
        return toAjax(sysEmailService.updateSysEmail(sysEmail));
    }

    /**
     * 删除sys_email
     */
    @RequiresPermissions("system:email:remove")
    @Log(title = "sys_email", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysEmailService.deleteSysEmailByIds(ids));
    }

    @Log(title = "sys_email", businessType = BusinessType.DELETE)
    @PostMapping( "/outRemove")
    @ResponseBody
    public AjaxResult outRemove(Long ids)
    {
        return toAjax(sysEmailService.outDeleteSysEmailById(ids));
    }

    @Log(title = "sys_email", businessType = BusinessType.DELETE)
    @PostMapping( "/shouRemove")
    @ResponseBody
    public AjaxResult shouRemove(Long ids)
    {
        return toAjax(sysEmailService.shouDeleteSysEmailById(ids));
    }

    //还原
    @Log(title = "sys_email", businessType = BusinessType.DELETE)
    @PostMapping( "/backRemove")
    @ResponseBody
    public AjaxResult backRemove(Long ids)
    {
        return toAjax(sysEmailService.backRemove(ids));
    }
}
